<template>
  <el-container class="main_container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" />
        <span>商城管理系统</span>
      </div>
      <div class="header_right">
        <div>常用地址</div>
        <div>
          <el-dropdown trigger="click">
            <el-badge :is-dot="caFlag" class="item">
              <i class="el-icon-message-solid" style="cursor: pointer;"></i>
            </el-badge>
            <el-dropdown-menu slot="dropdown">
              <div>
                <el-dropdown-item class="clearfix">
                  评论
                  <el-badge class="mark" :value="comment" />
                </el-dropdown-item>
              </div>
              <div>
                <el-dropdown-item class="clearfix">
                  回复
                  <el-badge class="mark" :value="answer" />
                </el-dropdown-item>
              </div>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div>
          <el-dropdown trigger="click">
            <i class="el-icon-user-solid"></i>
            <el-dropdown-menu slot="dropdown">
              <div>
                <el-dropdown-item class="clearfix">
                    个人信息
                </el-dropdown-item>
              </div>
              <div @click="logout">
                <el-dropdown-item class="clearfix">
                    退出
                </el-dropdown-item>
              </div>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </el-header>

    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu @open="handleOpen"
            @close="handleClose" background-color="#474e55" text-color="#fff"
            active-text-color="#4b96f5" :unique-opened="true"
            :collapse="isCollapse" :collapse-transition="false" :router="true"
            :default-active="activePath">
          <div class="toggle_button" @click="toggleCollapse" :style="leftMargin">
            <div style="font-size: 15px;">
              {{menuTitle}}
            </div>
            <div style="margin-right: 18px">
              <i :class="toggleFlag"></i>
            </div>
          </div>
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children"
                :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                <template slot="title">
                  <i :class="iconObj[subItem.id]"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
      <!-- 内容主体区 -->
      <el-container>
        <!-- 右侧内容 -->
        <el-main>
          <!-- 主页占位符 -->
          <router-view></router-view>
        </el-main>
        <!-- 下侧 -->
        <el-footer class="main_footer">Mall@Tomato</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default{
  created () {
    // 页面加载时获取菜单列表
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
    // 计算评论和回复的数量
    this.msgShow()
  },
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      // 图标
      iconObj: {
        '125': 'el-icon-setting',
        '103': 'el-icon-key',
        '101': 'el-icon-shopping-bag-1',
        '102': 'el-icon-document',
        '145': 'el-icon-s-data',
        '110': 'el-icon-user',
        '111': 'el-icon-orange',
        '112': 'el-icon-s-check',
        '104': 'el-icon-receiving',
        '115': 'el-icon-box',
        '121': 'el-icon-files',
        '107': 'el-icon-notebook-2',
        '146': 'el-icon-s-marketing'
      },
      // 是否折叠菜单，默认不折叠
      isCollapse: false,
      // 被激活的菜单index
      activePath: '',
      comment: 1,
      answer: 0,
      // 消息小圆点是否展示
      caFlag: false,
      toggleFlag: 'el-icon-s-fold',
      menuTitle: '菜单列表',
      leftMargin: 'margin-left: 30px'
    }
  },
  methods: {
    logout () {
    // 清空sessionStorage
      window.sessionStorage.clear()
      // 跳转到首页
      this.$router.push('/login')
    },
    async getMenuList () {
      const {data: res} = await this.$http.get('/menus')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.menuList = res.data
    },
    handleOpen (key, keyPath) {
    },
    handleClose (key, keyPath) {
    },
    // 点击按钮切换菜单的折叠和展开
    toggleCollapse () {
      if (!this.isCollapse) {
        // 折叠
        this.toggleFlag = 'el-icon-s-unfold'
        this.menuTitle = ''
        this.leftMargin = ''
      } else {
        // 展开
        this.toggleFlag = 'el-icon-s-fold'
        this.menuTitle = '菜单列表'
        this.leftMargin = 'margin-left: 30px'
      }
      this.isCollapse = !this.isCollapse
    },
    // 保存二级菜单的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 计算评论和回复的总数
    msgShow () {
      const total = this.comment + this.answer
      if (total > 0) {
        this.caFlag = true
      }
    }
  }
}
</script>

<style lang="less" scoped="scoped">
.main_container {
  height: 100%;
}
.el-header {
    background-color: #3f464c;
    color: #ffffff;
    font-size: 18px;
    text-align: center;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    > div {
      display: flex;
      align-items: center;
      img {
        height: 40px;
      }
      span {
        margin-left: 20px;
      }
    }
}
.el-icon-user-solid {
  margin-top: 20px;
  font-size: 40px;
  color: rgb(255, 255, 255);
  cursor: pointer;
}
.header_right {
  font-size: 15px;
  > div {
    margin-right: 20px;
  }
}
.el-dropdown-link {
  margin-left: 0 !important;
  color: #ffffff;
}
.item {
  margin-top: 0;
  margin-right: 5px;
  margin-bottom: 4px;
  height: 30px;
  line-height: 30px;
  font-size: 20px;
  color: #ffffff;
}
.el-aside {
  background-color: #474e55;
}
.toggle_button {
  background-color: #474e55;
  text-align: center;
  color: #fff;
  font-size: 30px;
  line-height: 25px;
  margin-top: 10px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
}
.main_footer {
  text-align: center;
  height: 35px !important;
  font-size: 14px;
}
</style>
